#import "HttpCommucationEngineer.h"
#import "Constant.h"
#import "StringUtil.h"
#import "DictionaryUtil.h"
#import "HttpDelegate.h"
#import "AlertViewEnginner.h"
static CHttpCommucationEngineer* iInstance=Nil;
@implementation CHttpCommucationEngineer
-init
{
	iHostUriPrefix=Nil;
	iCookie=Nil;
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (HandleHttpRequestMessage:)name:KNotificationHttpRequest object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (HandleHttpResponseMessage:)name:KNotificationHttpRecvResponse object:nil];
	return self;
}

- (void)dealloc {
	[iHostUriPrefix release];
	if(iCookie)
	{
		free(iCookie);
		iCookie=Nil;
	}
	[[NSNotificationCenter defaultCenter]removeObserver:self name:KNotificationHttpRequest object:nil];
	[[NSNotificationCenter defaultCenter]removeObserver:self name:KNotificationHttpRecvResponse object:nil];
	[super dealloc];
}

+ (CHttpCommucationEngineer*)GetIntance
{
	
	@synchronized(self)
	{
		if(!iInstance)
		{
			iInstance=[[CHttpCommucationEngineer alloc] init];
		}
	}
	return iInstance;
}

+ (void)Destroy
{
	@synchronized(self)
	{
		if(iInstance)
		{
			[iInstance release];
		}
	}
}


- (CHttpConnection *)HttpRequest:(NSString*)aUrl tag_Method:(int)aMethod tag_Data:(NSString*)aData tag_ControlId:(id)aControlId tag_Flag:(int)aFlag tag_Pos:(int)aPos
{
	CHttpConnection *conn=NULL;
	conn=[[CHttpConnection alloc] init];
	[[CAlertViewEnginner GetIntance] showWaitingDialog:@"从服务器获取数据"];
	[conn HttpRequest:aUrl tag_UriPrefix:@"" tag_Method:aMethod tag_Data:aData tag_ControlId:aControlId tag_Flag:aFlag tag_Pos:aPos];
	return conn;
}



- (void) HandleHttpRequestMessage:(NSNotification *)notify
{
	NSDictionary *aUserInfo;
	NSString *aUrl;
	NSString *aData;
	NSNumber *aMethod;
	int flag;
	id aControlId;
	int pos;
	pos=0;
	aUserInfo=[notify userInfo];
	aUrl=[aUserInfo objectForKey:@"url"];
	aData=[aUserInfo objectForKey:@"data"];
	aMethod=[aUserInfo objectForKey:@"method"];
	aControlId=[aUserInfo objectForKey:@"controlId"];
	flag=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"flag"];
	if(flag&4)
	{
		pos=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"pos"];
	}
	[self HttpRequest:aUrl tag_Method:[aMethod intValue] tag_Data:aData tag_ControlId:aControlId tag_Flag:flag tag_Pos:pos];
}

- (void) HandleHttpResponseMessage:(NSNotification *)notify
{
	NSDictionary *aUserInfo;
	int type;
	unsigned char *data;
	int size;
	int totalLength;
	int error;
	id delegate;
	aUserInfo=[notify userInfo];
	error=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"err"];
	delegate=[aUserInfo objectForKey:@"controlId"];
	if(error==0)
	{
	
		type=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"type"];
		switch(type)
		{
			case 0:
			{	
				[[CAlertViewEnginner GetIntance] closeWaitingDialog];
				HttpTextResponseData responseData;
				data=(unsigned char*)[CDictionaryUtil GetPointerValue:aUserInfo tagKey:@"data"];
				size=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"dataSize"];
				responseData.data=data;
				responseData.length=size;
				[delegate HttpTextResponse:&responseData];
				break;
			}
			case 1:
			{
				HttpStreamResponseData responseData;
				data=(unsigned char*)[CDictionaryUtil GetPointerValue:aUserInfo tagKey:@"data"];
				size=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"dataSize"];
				totalLength=[CDictionaryUtil GetIntValue:aUserInfo tagKey:@"totalSize"];
				responseData.data=data;
				responseData.length=size;
				responseData.totalLength=totalLength;
				[delegate HttpStreamResponseContinue:&responseData];
				break;
			}
			case 2:
			{
				[delegate HttpStreamResponseFinish:NULL];
				break;
			}
		}
	}else {
		[[CAlertViewEnginner GetIntance] closeWaitingDialog];
		[[CAlertViewEnginner GetIntance] Alert:@"网络异常"];
		//[delegate HttpResponseError:error];
	}

}
- (void)SetHostUriPrefix:(NSString*)aPrefix
{
	if(iHostUriPrefix)
	{
		[iHostUriPrefix	release];
		iHostUriPrefix=Nil;
	}
	[aPrefix retain];
	iHostUriPrefix=aPrefix;
}

- (void)SetCookie:(char *)aCookie
{
	if(iCookie)
	{
		free(iCookie);
		iCookie=Nil;
	}
	iCookie=aCookie;
}
@end
